package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.renren.modules.sys.entity.DormAreaEntity;
import io.renren.modules.sys.entity.DormFareEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.DormAreaService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.sys.entity.DormRepairEntity;
import io.renren.modules.sys.service.DormRepairService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Lizhiwei
 * @email 123@qq.com
 * @date 2022-10-11 19:43:49
 */
@RestController
@RequestMapping("sys/dormrepair")
public class DormRepairController  extends AbstractController{
    @Autowired
    private DormRepairService dormRepairService;
    @Autowired
    private DormAreaService dormAreaService;
    @Autowired
    private SysUserService sysUserService;



    @GetMapping("/getFixItem")
    public R getFixItem(@RequestParam("id") Integer id){
        Integer userId = getUserId().intValue();
        DormRepairEntity byId = dormRepairService.getById(id);
        if (byId.getStatus()!=0){
            return R.error("不可重复领取");
        }
        byId.setFixId(userId);
        byId.setStatus(1);
        dormRepairService.updateById(byId);
        return R.ok();
    }
    /**
     * 点击完成维修
     * @param dormRepair
     * @return
     */
    @PostMapping("/finish")
    public R finish(@RequestBody DormRepairEntity dormRepair){
        Integer userId = getUserId().intValue();
        dormRepair.setFixId(userId);
        dormRepair.setStatus(2);
        boolean b = dormRepairService.updateById(dormRepair);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:dormrepair:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dormRepairService.queryPage(params);


        List<DormRepairEntity> list = (List<DormRepairEntity>) page.getList();
        List<Object> list1 = list.stream().map((entity) -> {
            if (entity.getDormId() != null) {
                //获取真实宿舍号
                DormAreaEntity byId = dormAreaService.getById(entity.getDormId());
                if (byId!=null && byId.getName()!=null && !byId.getName().equals("")){
                    entity.setDormId(Integer.parseInt(byId.getName()));
                    //获取宿舍楼
                    DormAreaEntity byId1 = dormAreaService.getById(byId.getParentId());
                    entity.setSecondLevel(byId1.getName());
                    //获取宿舍区
                    DormAreaEntity byId2 = dormAreaService.getById(byId1.getParentId());
                    entity.setFirstLevel(byId2.getName());
                }

            }
            //获取维修工姓名
            if (entity.getFixId()!=null){
                SysUserEntity byId = sysUserService.getById(entity.getFixId());
                entity.setRealName(byId.getRealName());
            }
            return entity;
        }).collect(Collectors.toList());
        page.setList(list1);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list01")
//    @RequiresPermissions("sys:dormrepair:list")
    public R list01(@RequestParam Map<String, Object> params){
//        Integer userId = getUserId().intValue();

        PageUtils page = dormRepairService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:dormrepair:info")
    public R info(@PathVariable("id") Integer id){
		DormRepairEntity dormRepair = dormRepairService.getById(id);

        return R.ok().put("data", dormRepair);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:dormrepair:save")
    public R save(@RequestBody DormRepairEntity dormRepair){
        dormRepair.setCreateTime(new Date());
        dormRepair.setStatus(0);
		dormRepairService.save(dormRepair);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:dormrepair:update")
    public R update(@RequestBody DormRepairEntity dormRepair){
		dormRepairService.updateById(dormRepair);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:dormrepair:delete")
    public R delete(@RequestBody Integer[] ids){
		dormRepairService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
